import axios from "axios";
import router from "@/router";
import store from "@/store";
import { Message, Loading } from "element-ui";
import qs from "qs";
// 超时时间
axios.defaults.timeout = 100000;
// 跨域请求，允许保存cookie
axios.defaults.withCredentials = true;
// axios.defaults.headers = { "Content-Type": "application/json; charset=utf-8" };
axios.defaults.headers["Content-Type"] =
  "application/x-www-form-urlencoded; charset=utf-8";
// 非生产环境 && 开启代理, 接口前缀统一使用[/api]前缀做代理拦截!
const BASE_URL =
  process.env.NODE_ENV !== "production"
    ? process.env.VUE_APP_BASE_API
    : process.env.VUE_APP_SERVER_URL;
// 对面暴露的基础请求路径
axios.BASE_URL = BASE_URL;

/**
 * 请求拦截
 */
let loading;
axios.interceptors.request.use(
  (config) => {
    let showLoading = false;
    if (config.loading === true) {
      showLoading = true;
    }
    config.url = BASE_URL + config.url
    if(config.method == 'post'){
      config.headers['Content-Type'] = 'application/json'
    }
    if (showLoading) {
      loading = Loading.service({
        text: config.loadingText || "Loading...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

/**
 * 响应拦截
 */
axios.interceptors.response.use(
  (response) => {
    if (loading) {
      loading.close();
    }
   
    return response;
  },
  (error) => {
    if (loading) {
      loading.close();
    }
    Message({
      message: '服务器维护中！',
      type: "error",
      showClose: true,
      dangerouslyUseHTMLString: true,
      duration: 3000,
      customClass: "zZindex",
    })
    // if (error.response.status === 401) {
    //   // 超时自动刷新
    // } else if (error.response.status === 402 || error.response.status === 403) {
    //   Message({
    //     message: error.response.data.msg || error.response.data.exception,
    //     type: "error",
    //     showClose: true,
    //     dangerouslyUseHTMLString: true,
    //     duration: 3000,
    //     customClass: "zZindex",
    //   });
    // } else if (error.response.status === 404) {
    //   // 路径找不到
    //   Message({
    //     message: "404，路径找不到" + ":" + error.response.data.path,
    //     type: "error",
    //     showClose: true,
    //     duration: 3000,
    //   });
    // } else if (error.response.status === 504) {
    //   Message({
    //     message: "网络连接错误" + ":" + error.response.data,
    //     type: "error",
    //     showClose: true,
    //     duration: 3000,
    //     customClass: "zZindex",
    //   });
    // } else if (error.response.status === 502) {
    //   Message({
    //     message: "服务器连接失败" + ":" + "502",
    //     type: "error",
    //     showClose: true,
    //     duration: 3000,
    //     customClass: "zZindex",
    //   });
    // } else {
    //   Message({
    //     message:
    //       error.response.data.msg ||
    //       error.response.data ||
    //       error.response.data.exception ||
    //       error.response ||
    //       error,
    //     type: "error",
    //     showClose: true,
    //     duration: 5000,
    //     customClass: "zZindex",
    //   });
    // }

    return Promise.reject(error);
  }
);

export default axios